package wei.xinya.pattern.behavioral.strategy;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;

public enum CashType {
    NORMAL("normal", "正常", value -> value),
    DISCOUNT("discount", "八折", value -> value * 0.8),
    RETURN("return", "满300减100", value -> value < 300 ? value : value - Math.floor(value / 300) * 100);

    private final String type;
    private final String typeDesc;
    private final Function<Double, Double> function;

    public String getType() {
        return type;
    }

    public String getTypeDesc() {
        return typeDesc;
    }

    public Function<Double, Double> getFunction() {
        return function;
    }

    CashType(String type, String typeDesc, Function<Double, Double> function) {
        this.type = type;
        this.typeDesc = typeDesc;
        this.function = function;
    }

    public static CashType findByType(String type) {
        AtomicReference<CashType> en = new AtomicReference<>(null);
        Arrays.stream(CashType.values()).filter(e -> e.getType().equals(type)).findFirst().ifPresent(en::set);
        return en.get();
    }
}
